package com.ruoyi.project.data.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.data.service.IWeightWorkerService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.domain.WeightStation;
import com.ruoyi.project.data.service.IWeightStationService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 场站信息Controller
 *
 * @author ruoyi
 * @date 2020-03-21
 */
@RestController
@RequestMapping("/system/station")
public class WeightStationController extends BaseController
{
    @Autowired
    private IWeightStationService weightStationService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private IWeightWorkerService weightWorkerService;

    /**
     * 查询场站信息列表
     */
    @PreAuthorize("@ss.hasPermi('weight:station:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeightStation weightStation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightStation.setId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightStation.setAreas(areas);
        weightStation.setManagementCharacters(managementCharacters);
        startPage();
        List<WeightStation> list = weightStationService.selectWeightStationList(weightStation);
        return getDataTable(list);
    }

    /**
     * 导出场站信息列表
     */
    @PreAuthorize("@ss.hasPermi('weight:station:export')")
    @Log(title = "场站信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WeightStation weightStation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightStation.setId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightStation.setAreas(areas);
        weightStation.setManagementCharacters(managementCharacters);
        List<WeightStation> list = weightStationService.selectWeightStationList(weightStation);
        ExcelUtil<WeightStation> util = new ExcelUtil<WeightStation>(WeightStation.class);
        return util.exportExcel(list, "station");
    }

    /**
     * 获取场站信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('weight:station:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(weightStationService.selectWeightStationById(id));
    }

    /**
     * 新增场站信息
     */
    @PreAuthorize("@ss.hasPermi('weight:station:add')")
    @Log(title = "场站信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeightStation weightStation)
    {
        if(weightStation.getTown() == null || "".equals(weightStation.getTown())) {
            AjaxResult.error("请选择属地");
        }
        return toAjax(weightStationService.insertWeightStation(weightStation));
    }

    /**
     * 修改场站信息
     */
    @PreAuthorize("@ss.hasPermi('weight:station:edit')")
    @Log(title = "场站信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeightStation weightStation)
    {
        // 清楚场站人员特殊设备权限
        if (StringUtils.isNotBlank(weightStation.getIsSpecialEquipment()) && "0".equals(weightStation.getIsSpecialEquipment())){
            weightWorkerService.clearSpecialEquipmentPermissions(weightStation.getId());
        }
        return toAjax(weightStationService.updateWeightStation(weightStation));
    }

    /**
     * 删除场站信息
     */
    @PreAuthorize("@ss.hasPermi('weight:station:remove')")
    @Log(title = "场站信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(weightStationService.deleteWeightStationByIds(ids));
    }

    /**
     * 查询场站信息列表
     */
    @GetMapping("/getAreaList")
    public AjaxResult getAreaList(@Param(value="areaCode") String areaCode)
    {
        List<Map<String, Object>> list = weightStationService.getAreaList(areaCode);
        return AjaxResult.success(list);
    }

    /**
     * 附件上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {

            String path = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/seal", file);
            return AjaxResult.success(path);
        }
        return AjaxResult.error("请选择附件");
    }

    /**
     * 获取场站视频地址-预览
     * @param
     * @return
     */
    @GetMapping("/getVideoPath")
    public AjaxResult getVideoPath(@Param(value="stationId") Long stationId)
    {
       return  weightStationService.getVideoPath(String.valueOf(stationId));
    }

    /**
     *  获取场站视频地址-回放
     * @param stationId
     * @return
     */
    @GetMapping("/getBackVideoPath")
    public AjaxResult getBackVideoPath(@Param(value="stationId") Long stationId,@RequestParam(value = "startTime") String startTime)
    {
        return  weightStationService.getBackVideoPath(String.valueOf(stationId),startTime);
    }

    @GetMapping("/stationList")
    public AjaxResult stationList(WeightStation weightStation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightStation.setId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightStation.setAreas(areas);
        weightStation.setManagementCharacters(managementCharacters);
        List<WeightStation> list = weightStationService.selectStationGpsList(weightStation);
        return AjaxResult.success(list);
    }

    @GetMapping("/noWeightList")
    public TableDataInfo noWeightList(WeightStation weightStation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightStation.setId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightStation.setAreas(areas);
        weightStation.setManagementCharacters(managementCharacters);
        startPage();
        List<WeightStation> list = weightStationService.selectNoWeightStationList(weightStation);
        return getDataTable(list);
    }

}
